package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.po.ShoppingCartPO;
import com.sky.repository.ShoppingCartRepository;
import com.sky.service.DishService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Content:
 * @Author: Asen
 * @Date: 2023/9/8 15:32
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartRepository,ShoppingCartPO> implements ShoppingCartService {

    @Autowired
    private ShoppingCartRepository shoppingCartRepository;
    @Autowired
    @Lazy
    private DishService dishService;
    @Autowired
    @Lazy
    private SetmealService setmealService;
    /**
     * 添加购物车
     * @param source
     */
    @Override
    public void add(ShoppingCartDTO source) {
        /**
         * 菜品：(dishId=62, setmealId=null, dishFlavor=null)
         * 菜品带口味：(dishId=65, setmealId=null, dishFlavor=不要葱,微辣,半糖,常温)
         * 套餐：(dishId=null, setmealId=1700023245741969409, dishFlavor=null)
         */
        Long currentId = BaseContext.getCurrentId();
        //判断当前加入到购物车中的商品是否已经存在了
        ShoppingCartPO shoppingCartPO = shoppingCartRepository.querybyCondition(source,currentId);
        if(ObjectUtil.isNotNull(shoppingCartPO)){
            //如果已经存在了,只需要数量+1
            shoppingCartPO.setNumber(shoppingCartPO.getNumber()+1);
            shoppingCartRepository.updateById(shoppingCartPO);
        }else {
            shoppingCartPO = new ShoppingCartPO();
            //如果不存在,新增数据
            if(ObjectUtil.isNotNull(source.getDishId())){//新增的是菜品
                DishVO dishVO = dishService.getByIdWithFlavor(source.getDishId());
                shoppingCartPO.setDishId(dishVO.getId());
                shoppingCartPO.setImage(dishVO.getImage());
                shoppingCartPO.setDishFlavor(source.getDishFlavor());
                shoppingCartPO.setUserId(currentId);
                shoppingCartPO.setNumber(1);
                shoppingCartPO.setAmount(dishVO.getPrice());
                shoppingCartPO.setName(dishVO.getName());
            }
            if(ObjectUtil.isNotNull(source.getSetmealId())){//新增的是套餐
                SetmealVO setmealVO = setmealService.getById(source.getSetmealId());
                shoppingCartPO.setSetmealId(setmealVO.getId());
                shoppingCartPO.setImage(setmealVO.getImage());
                shoppingCartPO.setUserId(currentId);
                shoppingCartPO.setNumber(1);
                shoppingCartPO.setAmount(setmealVO.getPrice());
                shoppingCartPO.setName(setmealVO.getName());
            }
            shoppingCartRepository.insert(shoppingCartPO);
        }
    }

    /**
     * 获取当前用户购物车内容
     * @return
     */
    @Override
    public List<ShoppingCartPO> list() {
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartRepository.selectList(new LambdaQueryWrapper<ShoppingCartPO>()
                .eq(ShoppingCartPO::getUserId,currentId));
    }

    /**
     * 减少购物车物品
     * @param source
     */
    @Override
    public void sub(ShoppingCartDTO source) {
        /**
         * 菜品：(dishId=62, setmealId=null, dishFlavor=null)
         * 菜品带口味：(dishId=65, setmealId=null, dishFlavor=不要葱,微辣,半糖,常温)
         * 套餐：(dishId=null, setmealId=1700023245741969409, dishFlavor=null)
         */
        Long currentId = BaseContext.getCurrentId();
        //查询购物车物品数量
        ShoppingCartPO shoppingCartPO = shoppingCartRepository.querybyCondition(source,currentId);
        if(shoppingCartPO.getNumber()>1){
            //只需要数量-1即可
            shoppingCartPO.setNumber(shoppingCartPO.getNumber()-1);
        }else {
            //需要删除该条数据
            shoppingCartRepository.deleteById(shoppingCartPO.getId());
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartRepository.delete(new LambdaQueryWrapper<ShoppingCartPO>()
                .eq(ShoppingCartPO::getUserId,currentId));
    }

    /**
     * 批量插入购物车信息
     */
    public void saveShoppingCart(List<ShoppingCartPO> source){
        this.saveBatch(source);
    }
}
